package com.dementors.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.dementors.R;
import com.dementors.base.BaseActivity;
import com.dementors.qrcode.CaptureActivity;
import com.dementors.ui.fragment.FragmentLoops;
import com.dementors.ui.fragment.FragmentMine;
import com.dementors.ui.fragment.FragmentSea;
import com.dementors.ui.fragment.FragmentShopping;
import com.dementors.ui.fragment.FragmentToday;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class MainActivity extends BaseActivity
    implements RadioGroup.OnCheckedChangeListener {

  @Bind(R.id.toolbar) Toolbar toolbar;
  private RadioGroup rGroup;
  private RadioButton lastCheckedRbtn;// 记录上一次点击的RadioButton
  private FragmentManager manager;
  private List<Fragment> fragments;
  private FragmentToday f1 = new FragmentToday();
  private FragmentSea f2 = new FragmentSea();
  private FragmentLoops f3 = new FragmentLoops();
  private FragmentShopping f4 = new FragmentShopping();
  private FragmentMine f5 = new FragmentMine();

  @Override protected void getIntentData(Bundle savedInstanceState) {

  }

  @Override protected int provideContentViewId() {
    return R.layout.activity_main;
  }

  @Override protected void initView() {
    rGroup = (RadioGroup) findViewById(R.id.rGroup);
    lastCheckedRbtn = (RadioButton) findViewById(R.id.rbtn_today);

    toolbar.setTitle("");
    setSupportActionBar(toolbar);
  }

  @Override protected void setListener() {
    rGroup.setOnCheckedChangeListener(this);
  }

  @Override protected void initData() {
    initRadioButton(true, getResources().getColor(R.color.rbtnCheckTextColor));
    initFragment();
  }

  /**
   * 初始化fragment
   */
  private void initFragment() {
    fragments = new ArrayList<>();
    fragments.add(f1);
    fragments.add(f2);
    fragments.add(f3);
    fragments.add(f4);
    fragments.add(f5);
    manager = getSupportFragmentManager();

    FragmentTransaction ft = manager.beginTransaction();
    ft.add(R.id.framelayout, fragments.get(0), "0");
    ft.commit();
  }

  /**
   * 初始化radiobutton
   *
   * @param checked 被选择的RadioButton
   * @param color 被选择的RadioButton下方文字的颜色
   */
  private void initRadioButton(boolean checked, int color) {
    lastCheckedRbtn.setChecked(checked);
    lastCheckedRbtn.setTextColor(color);
  }

  @Override public void onCheckedChanged(RadioGroup group, int checkedId) {
    RadioButton rBtn = (RadioButton) findViewById(checkedId);
    rBtn.setTextColor(getResources().getColor(R.color.rbtnCheckTextColor));
    initRadioButton(false, getResources().getColor(R.color.rbtnUnCheckTextColor));
    int index = Integer.parseInt(rBtn.getTag().toString());
    int lastIndex = Integer.parseInt(lastCheckedRbtn.getTag().toString());
    FragmentTransaction ft = manager.beginTransaction();
    Fragment fragment = manager.findFragmentByTag(index + "");

    if (fragment == null) {
      ft.add(R.id.framelayout, fragments.get(index), index + "");
    } else {
      ft.show(fragments.get(index));
    }
    ft.hide(fragments.get(lastIndex));
    ft.commit();
    lastCheckedRbtn = rBtn;
  }

  @Override public void onClick(View v) {

  }

  @Override public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
      case R.id.slidemenu:
        Intent intent = new Intent(this, CaptureActivity.class);
        startActivity(intent);
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
